{extend name="base:base" /}


{block name="body"}


<style>
    input{
        border-color: #e6e6e6;
    }
    input{
        height: 38px;
        line-height: 1.3;
        line-height: 38px\9;
        border-width: 1px;
        border-style: solid;
        background-color: #fff;
        border-radius: 2px;
        padding-left: 10px;
    }
    input:hover, textarea:hover {
        border-color: #D2D2D2!important;
    }
    input, textarea {
        padding-left: 10px;
    }
    textarea {
        padding-top: 10px;
    }


</style>


<div class="container">

    <?php
    $testUrl = APP_HOST .APP_URL. '/' . $result['action_val'];
    ?>


    <table class="table">
        <tr>
            <th width='130'>路径</th>
            <th><?php echo ($_GET['controller']); ?> &gt; <?php echo ($_GET['method']); ?></th>
        </tr>
        <tr>
            <th>标题</th>
            <td><?php echo $result['title']; ?></td>
        </tr>
        <tr>
            <th>Action</th>
            <td>                
                <input name="action_val" type="text" id="action_val" value="<?php echo $testUrl; ?>" style="width:90%"  /> 
                <button id="openActionUrl" class="btn btn-default" >GET</button></td>
        </tr>
        <tr>
            <th>Method</th>
            <td><input name="method_val" type="text" id="method_val" value="<?php echo $result['method_val']; ?>" /></td>
        </tr>
        <tr>
            <th>Headers</th>
            <td>

                <div style='margin-bottom:5px;'>  Value : <input type='text' id='uid' style='width:300px' value=''/>  tk-uid </div>
                <div style='margin-bottom:5px;'>  Value : <input type='text' id='token' style='width:300px' value=''/> tk-token </div>                

            </td>
        </tr>

        <tr>
            <th>Data</th>
            <td><?php
                echo $result['key_val_html'];
                ?></td>
        </tr>
        <tr>
            <th>提交</th>
            <td><button type="button" name="do_test" id="do_test" class="btn btn-primary " >提交测试</button>  
                <a class="btn btn-default " href="javascript:history.back();"> 《 返回</a></td>
        </tr>
        <tr>
            <th>结果</th>
            <td>
                <textarea name="result" id="result" style="width:100%; height:400px;"></textarea></td>
        </tr>
    </table>




</div>

{/block}



{block name="foot_js"} 




<script type='text/javascript' src="__PUBLIC__/libs/md5/md5.js"></script>
<script type='text/javascript'>

    //初始化URL地址
    var testUrl = '<?php echo $testUrl; ?>';


    // JavaScript Document
    $(document).ready(function () {

        $('#openActionUrl').click(
                function () {
                    winOpen($('#action_val').val());
                }
        );

        // var time = Date.parse(new Date());
        // time = time / 1000;
        // var sgin = hex_md5('?token=' + token + '&time=' + time); 

        var header = {"tk-uid": $('#uid').val(), "tk-token": $('#token').val()};


        $('#do_test').click(function () {
            apiTest(header);
        });
    });
    function apiTest(header) {

        var headers = arguments[0] || {};

        var action_dom = $('#action_val');
        var method_dom = $('#method_val');


        var keys = new Array();
        var vals = new Array();
        var data = '';

        $('input[name=paramKey]').each(function () {
            var key = $.trim($(this).val());
            keys.push(encodeURIComponent(key));
        });
        $('input[name=paramVal]').each(function () {
            var val = $.trim($(this).val());
            vals.push(encodeURIComponent(val));
        });
        for (var i = 0; i < keys.length; i++) {
            data += keys[i] + '=' + vals[i] + '&';
        }

        //截取最后一个字符 
        data = data.substring(0, data.length - 1);
        // action_dom.val(testUrl + '?' + decodeURIComponent(data));

        $.ajax({
            headers: headers,
            type: method_dom.val(),
            url: action_dom.val(),
            data: data,
            dataType: 'html',
            success: function (msg) {
                //上线后
                $('#result').val(formatJson(msg));
            }, error: function (XMLHttpRequest, textStatus, errorThrown) {
                $('#result').val(errorThrown);
            }
        });
    }
    /*
     * StringBuffer class
     */
    function StringBuffer() {
        this.buffer = [];
    }
    StringBuffer.prototype.append = function append(string) {
        this.buffer.push(string);
        return this;
    };
    StringBuffer.prototype.toString = function toString() {
        return this.buffer.join("");
    };
    /*
     * Strip html tags
     */
    function stripTags(str) {
        return str.replace(/<[^>].*?>/g, "");
    }
    /*
     * Display json with better look
     */
    function formatJson(str) {
        var tab = 0;
        var buf = new StringBuffer();
        for (var i = 0; i < str.length; i++) {
            var char = str.charAt(i);
            if (char == '{' || char == '[') {
                tab++;
                char = char + "\n";
                for (var j = 0; j < tab; j++)
                    char = char + "\t";
            }
            if (char == '}' || char == ']') {
                tab--;
                for (var j = 0; j < tab; j++)
                    char = "\t" + char;
                char = "\n" + char;
            }
            if (char == ',') {
                char = char + "\n";
                for (var j = 0; j < tab; j++)
                    char = char + "\t";
            }
            buf.append(char);
        }
        return buf.toString();
    }


    function winOpen(url) {
        childWindow = window.open(url, "Test", "width=1000,height=520,menubar=0,scrollbars=1, resizable=1,status=1,titlebar=0,toolbar=0,location=1");
    }


</script>

{/block}